package Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.DatabaseDAO;
import Flights.Flight;
import Flights.Flights;
import Flights.Passenger;
import Flights.Passengers;

/**
 * Servlet implementation class CancelServlet
 */
@WebServlet("/CancelServlet")
public class CancelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CancelServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext ctx = getServletContext();
		HttpSession session = request.getSession();
		
		PrintWriter out = response.getWriter(); 
		
		String client = request.getParameter("client");
		if(client==null){
			client="web";
		}
		
		String flightId = request.getParameter("flightId");
		String btn = request.getParameter("submit");
		if(btn==null){
			btn="";
		}
		
		if(btn.equals("") && client.equals("web")){
			Flights flights = (Flights) session.getAttribute("flights");
			Passengers passengers = (Passengers) session.getAttribute("passengers");
			
			for(Flight f : flights.getAllFlights()){
				if(f.getIdLeta().equals(flightId)){
					flights.setFlight(f);
				}
			}
			
			session.setAttribute("flights", flights);
			session.setAttribute("passengers", passengers);
			
			RequestDispatcher disp = getServletContext().getRequestDispatcher("/otkazi.faces");
			disp.forward(request, response);
			return;
		} else if (btn.equals("Potvrdi") || client.equals("CommandPrompt")){
			String  idLeta = request.getParameter("idLeta");
			String  JMBG = request.getParameter("JMBG");
			
			Flights flights = new Flights(ctx.getRealPath("") + "/listaLetova/flights.txt");
			Flight flight = flights.getFlights().get(idLeta);
			if(flight==null){
				out.println("Ne postoji let: " + idLeta);
				out.close();
				return;
			}
			
			Passengers passengers = new Passengers(ctx.getRealPath("")+"/listaLetova/" + idLeta + ".txt");
			Passenger passenger = passengers.getPassengersMap().get(JMBG);
			if(passenger==null){
				out.println("Ne postoji putnik sa JMBGom: " + JMBG + " na letu: " + idLeta);
				out.close();
				return;
			}
			
			if(!flight.isOtkazi()){
				out.println("Isteklo vreme otkaza. Avion: " + idLeta + " polece za manje od 24 sata.");
				out.close();
				return;
			}
			
			String passengerString = JMBG + ";" + passenger.getIme() + ";" + passenger.getPrezime();
			
			DatabaseDAO dao = DatabaseDAO.getInstance();
			dao.delete(passengerString, ctx.getRealPath("")+"/listaLetova/" + idLeta + ".txt");
			
			//kreiraj string sa podacima o letu
			String flightString = flight.getIdLeta() + ";" + 
					flight.getVremePolaskaString() + ";" + 
					flight.getVremeDolaskaString() + ";" + 
					flight.getPolaziAerodrom() + ";" + 
					flight.getOdrediste() + ";" + 
					flight.getBrojPutnika();
			
			dao.delete(flightString, ctx.getRealPath("")+"/listaPutnika/" + JMBG + ".txt");
			
			if(client.equals("CommandPrompt")){
				out.println("Uspesno ste otkazali putnika sa JMBGom: " + JMBG + " na letu: " + idLeta);
				out.close();
			} else {
				RequestDispatcher disp = getServletContext().getRequestDispatcher("/sediste_otkazano.faces");
				disp.forward(request, response);
			}
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
